﻿using System;
using System.IO;
using System.Data;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.Net.Mail;
using System.Net.Configuration;
using System.Web.Configuration;

namespace LenderClearingHouse
{
  public partial class AvailableInvestments : GridPage
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      listFilter.SearchButton.Click += new ImageClickEventHandler(SearchButton_Click);
      RadAjaxManager.AjaxSettings.AddAjaxSetting(Grid, Grid);

      string postBackerID = Request.Form.Get("__EVENTTARGET");
      string postBackerArg = Request.Form.Get("__EVENTARGUMENT");
      if (postBackerArg == "Refresh") Grid.Rebind();
    }

    void SearchButton_Click(object sender, ImageClickEventArgs e)
    {
      Grid.Rebind();
      listFilter.ClearFilter();
    }

    protected override SourcePage GetSourcePage()
    {
      return CurrentUser.SubRole == UserSubRole.LVReo ? SourcePage.LvreoList : SourcePage.SdreoList;
    }

    protected override SqlDataSource GetSqlDataSource()
    {
      return sqlDataSource;
    }

    protected override ListFilterControl GetListFilterControl()
    {
      return listFilter;
    }

    protected override Control GetGridContainer()
    {
      return phGrid;
    }

    protected override string GetDataSourceID()
    {
      return sqlDataSource.ID;
    }

    protected override void SetupSqlDataSource(SqlDataSource dataSource)
    {
      base.SetupSqlDataSource(dataSource);

      dataSource.SelectParameters.Add(new Parameter()
      {
        ConvertEmptyStringToNull = false,
        Name = "currentUserId",
        Type = TypeCode.Int32
      });

      dataSource.Selecting += new SqlDataSourceSelectingEventHandler(dataSource_Selecting);
    }

    void dataSource_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
    {
      e.Command.Parameters["@currentUserId"].Value = CurrentUser.UserId;
    }

    protected override void CreateColumns(GridColumnCollection columns)
    {
      GridTemplateColumn gtc = new GridTemplateColumn()
      {
        UniqueName = "Mail",
        HeaderText = "",
        ItemTemplate = new MailColumn(),
        AllowFiltering = false
      };
      gtc.HeaderStyle.Width = Unit.Pixel(30);
      columns.Add(gtc);

      base.CreateColumns(columns);
    }

    class MailColumn : IBindableTemplate
    {
      public void InstantiateIn(Control container)
      {
        ImageButton btn = new ImageButton();        
        btn.DataBinding += new EventHandler(btn_DataBinding);

        container.Controls.Add(btn);
      }

      void btn_DataBinding(object sender, EventArgs e)
      {
        ImageButton btn = (ImageButton)sender;
        GridDataItem container = (GridDataItem)btn.NamingContainer;
        DataRowView drw = container.DataItem as DataRowView;
        if (drw == null) return;

        if (!(bool)drw["Requested"])
        {
          btn.ImageUrl = "~/images/mail2_16x16.gif";
          btn.OnClientClick = "RequestProperty('" + drw["Apn"].ToString() + "');";
        }
        else
        {
          btn.ImageUrl = "~/images/mail2_16x16_disabled.gif";
          btn.Enabled = false;
        }        
      }

      public System.Collections.Specialized.IOrderedDictionary ExtractValues(Control container)
      {
        OrderedDictionary od = new OrderedDictionary();
        return od;
      }
    }
  }
}
